package com.ruiao.tools.youyan;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarEntry;
import com.loopj.android.http.RequestParams;
import com.ruiao.tools.R;
import com.ruiao.tools.url.URLConstants;
import com.ruiao.tools.utils.HttpUtil;
import com.ruiao.tools.utils.StatusBarUtil;
import com.ruiao.tools.utils.ToastHelper;

import cz.msebera.android.httpclient.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.baidu.mapapi.BMapManager.getContext;

public class YouYanHistryActivity extends AppCompatActivity {
    YouYanBarChartManager manager  ;
    DateFormat bf = new SimpleDateFormat("yyyy年MM月dd日");
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat format2 = new SimpleDateFormat("dd日HH时");
    Date date = new Date();

    @BindView(R.id.tv_city)
    TextView tvCity;
    YouyanGpsBean bean;
    @BindView(R.id.tv_diqu)
    TextView tvDiqu;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_jinghuaqi)
    TextView tvJinghuaqi;
    @BindView(R.id.tv_fengji)
    TextView tvFengji;

    @BindView(R.id.Chart)
    BarChart Chart;
    ArrayList<YouyanNowBean> list = new ArrayList<>();
    ArrayList<BarEntry> chart_list = new ArrayList<>();
    ArrayList<String> times = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_yan_histry);
        ButterKnife.bind(this);
        StatusBarUtil.darkMode(this);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        bean = (YouyanGpsBean) bundle.getSerializable("bean");
        tvDiqu.setText(bean.name);
        tvNum.setText(bean.num);
        tvJinghuaqi.setText(bean.jinghuaqi);
        tvFengji.setText(bean.fengji);
        manager = new YouYanBarChartManager(Chart);
        manager.initLineChart("");
        initDataDay();
    }

    //今日小时数据
    private void initDataDay() {
        RequestParams pa = new RequestParams();
        pa.add("username", "15801299706");
        pa.add("MonitorID", bean.MonitorID);


        pa.add("start", format1.format(new Date(date.getTime() -1000 * 60 * 60 * 12)));
        pa.add("end", format1.format(date));
        pa.add("type", "xiaoshi");
        list.clear();
        HttpUtil.get(URLConstants.YOUYAN_HISTROY, pa, new HttpUtil.SimpJsonHandle(getContext()) {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                try {
                    if (response.getBoolean("success")) {
                        chart_list . clear();
                        times.clear();
                        JSONArray array = response.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.getJSONObject(i);
//                            YouyanNowBean bean = new YouyanNowBean();
//                            bean.nongdu = obj.getString("num");
//                            bean.time = obj.getString("time");
//                            bean.fengji = obj.getString("fengji");
//                            bean.jinghuaqi = obj.getString("jinghuaqi");
                            chart_list.add(new BarEntry(i, Float.parseFloat( obj.getString("num"))));

                            Date date = format1.parse(obj.getString("time"));

                            times.add(format2.format(date));
                        }

                        manager.setTimes(times);
                        manager.setData(chart_list);

                    } else {
                        ToastHelper.shortToast(getContext(), response.getString("message"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @OnClick({R.id.ll_shifenzhong, R.id.ll_xiaoshi, R.id.ll_zhou})
    public void onViewClicked(View view) {
        Intent intent ;
        switch (view.getId()) {

            case R.id.ll_shifenzhong:
                intent = new Intent(this,HistoryShifenActivity.class);
                intent.putExtra("MonitorID",bean.MonitorID);
                startActivity(intent);
                break;
            case R.id.ll_xiaoshi:
                intent = new Intent(this,HistroyShiActivity.class);
                intent.putExtra("MonitorID",bean.MonitorID);
                startActivity(intent);
                break;
            case R.id.ll_zhou:
                intent = new Intent(this,HistroyDayActivity.class);
                intent.putExtra("MonitorID",bean.MonitorID);
                startActivity(intent);
                break;
        }
    }
    //某个小时 内的 10 分钟数据
//    private void initDataXiaoshi(){
//        RequestParams pa = new RequestParams();
//        pa.add("username", "15801299706");
//        pa.add("MonitorID", bean.MonitorID);
//        list.clear();
//        HttpUtil.get(URLConstants.YOUYAN_HISTROY, pa, new HttpUtil.SimpJsonHandle(getContext()) {
//            @Override
//            public void onStart() {
//                super.onStart();
//            }
//
//            @Override
//            public void onFinish() {
//                super.onFinish();
//
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//                super.onSuccess(statusCode, headers, response);
//                try {
//                    if (response.getBoolean("success")) {
//                        JSONArray array =  response.getJSONArray("data");
//                        for(int i = 0; i < array.length() ; i++){
//                            JSONObject obj = array.getJSONObject(i);
//                            YouyanBean bean  = new YouyanBean();
//                            bean.nongdu = obj.getString("num");
//                            bean.time = obj.getString("time");
//                            bean.fengji = obj.getString("fengji");
//                            bean.jinghuaqi = obj.getString("jinghuaqi");
//                            table_list.add(bean);
//                        }
//                        table.setData(table_list);
//                    } else {
//                        ToastHelper.shortToast(getContext(), response.getString("message"));
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });
//
//    }
}
